package com.me.spider;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.me.common.util.io.ImageUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;

/**
 * htmlunit 爬取图片
 *
 * @author ME
 * @date   2019/12/30
 */
public class ImageDownload {

    private String[] directions = new String[]{"d", "l", "f", "r", "b", "u"};
    private int smallShard = 2;
    private String smallCategory = "l1";
    private int bigShard = 5;
    private String bigCategory = "l2";

    public static void main(String[] args) throws Exception {
        String baseUrl = "https://ca.dg53.xyz/htm_data/2103/16/4368172.html";
        String selector = "div.tpc_content.do_not_catch > div.image-big > img";
        String localPath = "D:\\装机\\dev\\ME\\图";

        try (final WebClient webClient = new WebClient()) {
            final HtmlPage page = webClient.getPage(baseUrl);
            String title = page.querySelector("#main > div > table > tbody > tr.tr1.do_not_catch > th > table > tbody > tr > td > h4").asText();

            // get list of all divs
            final DomNodeList<DomNode> divs = page.querySelectorAll(selector);
            for (DomNode div : divs) {

            }

            //get div which has the id 'breadcrumbs'
            final DomNode div = page.querySelector("div#breadcrumbs");
        }
    }

    private void initUrl(String baseUrl, String baseLocal, int type) throws Exception {
        int shard = 0;
        String category = "";
        if (type == 1) {
            category = smallCategory;
            shard = smallShard;
        } else if (type == 2) {
            category = bigCategory;
            shard = bigShard;
        }
        for (String direction : directions) {
            for (int i = 1; i <= shard; i++) {
                for (int j = 1; j <= shard; j++) {
                    String fileName = category + "_" + direction + "_" + i + "_" + j + ".jpg";
                    // mres_b/l1/2/l1_b_2_2.jpg
                    String urlStr = baseUrl + "mres_" + direction + "/" + category + "/" + i + "/" + fileName;
                    ImageUtils.download(urlStr, baseLocal, fileName);
                }
            }
        }
    }

}
